<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="图形码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="验证码" prop="msg">
        <el-input v-model="form.msg" />
        <el-button v-if="totalTime===7" @click="getCode">获取验证码</el-button>
        <el-button v-else disabled>获取验证码(倒计时:{{ totalTime }})</el-button>
      </el-form-item>
    </el-form>
    <!-- <el-button type="primary" @click="submit">确定</el-button>
    <el-button type="primary" @click="avatarCheck">部分表单验证</el-button> -->

  </div>
</template>
<script>

export default {
  components: {

  },
  data() {
    return {
      totalTime: 7,
      form: {
        mobile: '',
        code: '',
        msg: ''
      },
      rules: {
        mobile: [{ required: true, message: '必填', trigger: 'change' }],
        code: [{ required: true, message: '必填', trigger: 'change' }],
        msg: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    getCode() {
      let count = 0
      this.$refs.form.validateField(['mobile', 'code'], (errorMessage) => {
        if (errorMessage === '') {
          count++
          if (count === 2) {
            console.log('验证成功')
            // 倒计时
            this.totalTime--
            const _id = setInterval(() => {
              this.totalTime--
              if (this.totalTime <= 0) {
                clearInterval(_id)
                this.totalTime = 7
              }
              console.log(this.totalTime)
            }, 1000)
          }
        } else {
          console.log('失败')
        }
      })
    }
  }
}
</script>
<style>
</style>
